package com.dapeis.web.admin.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.dao.PagerHelper;
import com.ag.module.admin.domain.AdminUser;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Bank;
import com.dapeis.domain.Collocation;
import com.dapeis.domain.CollocationInformation;
import com.dapeis.domain.ext.CollocationExt;
import com.dapeis.service.BankService;
import com.dapeis.service.CollocationInformationService;
import com.dapeis.service.CollocationService;
import com.dapeis.service.JPushService;
import com.dapeis.tx.push.AuditPassNoticeText;
import com.dapeis.tx.push.AuditRefuseNoticeText;

/**
 * 搭配师管理 Title:AdminCollocationController.java AdminCollocationController
 * Description:
 * 
 * @author yuanyong
 * @date 2017年4月20日 下午1:58:36 Email: hzyuanyong@126.com
 *
 */
@Controller
@RequestMapping(value = "/admin/collocation")
public class AdminCollocationController extends AdminController {

    @Resource
    CollocationService collocationService;
    @Resource
    CollocationInformationService collocationInformationService;
    @Resource
    JPushService jpushService;
    @Resource
    BankService bankService;

    /**
     * 所有待审核的搭配师列表
     * 
     * @param pager
     * @param model
     * @param attr
     * @return
     */
    @RequestMapping("/auditList")
    public String auditCollocationList(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	if (null == pager) {
	    pager = new Pager();
	}
	Example example = new Example(Collocation.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(Collocation.PROPERTY_AUDITSTATE,
		StylistConstants.Audit.WAIT_AUDIT);
	pager.setExample(example);
	collocationService.pager(pager);
	PagerHelper.pager(pager, collocationService.getCollocationExtMapper());
	model.put("pager", pager);
	return doPackageURI("collocation/audit_wait_list");
    }

    /**
     * 查看审核资料
     * 
     * @param collocationId
     * @param model
     * @param attr
     * @return
     */
    @RequestMapping(value = "/{collocationId}/detail")
    public String detail(@PathVariable Integer collocationId,
	    Map<Object, Object> model, RedirectAttributes attr) {
	CollocationExt ext = collocationService.getCollocationExtMapper()
		.selectByPrimaryKey(collocationId);
	AgAssert.notNull(ext.getCollocationInformation(),
		SysTipsException.class, "无法查找搭配师审核信息:" + collocationId);
	model.put("collocation", ext);
	return doPackageURI("collocation/audit_detail");
    }

    /**
     * 银行卡信息
     * 
     * @param collocationId
     * @param model
     * @param attr
     * @return
     */
    @RequestMapping(value = "/{collocationId}/bank")
    public String bank(@PathVariable Integer collocationId,
	    Map<Object, Object> model, RedirectAttributes attr) {
	Collocation collocation = collocationService
		.selectByPrimaryKey(collocationId);
	AgAssert.notNull(collocation, SysTipsException.class, "找不到搭配师信息ID:"
		+ collocationId);
	Bank bank = null;
	if (null != collocation.getBankId()) {
	    bank = bankService.selectByPrimaryKey(collocation.getBankId());
	}
	model.put("collocation", collocation);
	model.put("bank", bank);
	return doPackageURI("collocation/bank");
    }

    @RequestMapping(value = "/{collocationId}/bankSave")
    public String bankSave(@PathVariable Integer collocationId,
	    @ModelAttribute Bank bank, Map<Object, Object> model,
	    RedirectAttributes attr, HttpSession session) {
	Bank dbBank = bankService.queryBankByCollocaitonId(collocationId);
	AdminUser adminUser = getSessionAdminUser(session);
	CollocationInformation colloctInformation = collocationInformationService
		.getCollocaitonInformationByCollocationId(collocationId);
	/*if (!(colloctInformation.getRealName().equals(bank.getName()))) {
	    setErrorMsg("开户人姓名与提交审核人信息不一致!");
	    return this.redirect("/admin/collocation/{collocationId}/bank");
	}*/
	Bank bankInfo = null;
	if (null == dbBank) {
	    bankInfo = new Bank();
	    bankInfo.initBank(adminUser.getId(), collocationId, bank);
	    bankService.insertSelective(bankInfo);
	} else {
	    bankInfo = dbBank.initBank(adminUser.getId(), collocationId, bank);
	    bankService.updateByPrimaryKeySelective(bankInfo);
	}
	Collocation collocation = collocationService
		.selectByPrimaryKey(collocationId);
	collocation.setBankId(bankInfo.getId());
	collocationService.updateByPrimaryKeySelective(collocation);
	setInfoMsg("操作成功！", attr);
	return redirect("/admin/collocation/list");
    }

    @RequestMapping("/add")
    public String add(RedirectAttributes attr) {
	return doPackageURI("collocation/edit");
    }

    @RequestMapping("/{collocationId}/edit")
    public String edit(@PathVariable Integer collocationId,
	    Map<Object, Object> model, RedirectAttributes attr) {
	Collocation collocation = collocationService
		.selectByPrimaryKey(collocationId);
	if (null == collocation) {
	    throw new RuntimeException("找不到搭配师 Id： " + collocationId);
	}
	model.put("collocation", collocation);
	return this.add(attr);
    }

    /**
     * 审核是否通过,更新审核状态
     * 
     * @param collocationId
     * @param audit
     * @param model
     * @param attr
     * @return
     */
    @RequestMapping(value = "doAudit/{collocationId}/{audit}/{errType}")
    public String doAudit(@PathVariable Integer collocationId,
	    @PathVariable String audit, @PathVariable String errType,
	    Map<Object, Object> model, RedirectAttributes attr,
	    HttpSession session) {
	Collocation collocation = collocationService
		.selectByPrimaryKey(collocationId);
	CollocationInformation information = collocationInformationService
		.getCollocaitonInformationByCollocationId(collocationId);
	AgAssert.isTrue(
		collocation.getAuditState() != StylistConstants.Audit.PASS_AUDIT,
		SysTipsException.class, "请勿重复审核!");
	AdminUser adminUser = getSessionAdminUser(session);
	AgAssert.notNull(collocation, SysTipsException.class, "无法查找搭配师信息:"
		+ collocationId);
	String url = "";
	if (audit.equals("pass")) {
	    url = "/admin/collocation/{collocationId}/bank";
	    setInfoMsg("审核成功,请补充银行卡信息！", attr);
	    collocation.setAuditState(StylistConstants.Audit.PASS_AUDIT);
	} else if (audit.equals("fail")) {
	    url = "/admin/collocation/list";
	    setInfoMsg("审核成功！", attr);
	    information.setAuditRemark(getRemark(errType));
	    collocation.setAuditState(StylistConstants.Audit.REJECT_AUDIT);
	}
	information.setAdminUserId(adminUser.getId());
	information.setAuditTime(new Date().getTime() / 1000);

	collocationService.updateByPrimaryKeySelective(collocation);
	collocationInformationService.updateByPrimaryKeySelective(information);

	// 推送和发送短信
	jpushService.pushMessage(collocationId,
		"pass".equals(audit) ? new AuditPassNoticeText()
			: new AuditRefuseNoticeText());
	return redirect(url);
    }

    @RequestMapping("/{collocationId}/checkState")
    public String checkState(@PathVariable Integer collocationId,
	    Map<Object, Object> model, RedirectAttributes attr,
	    HttpServletRequest request) {
	Collocation collocation = collocationService
		.selectByPrimaryKey(collocationId);
	if (null == collocation) {
	    throw new RuntimeException("找不到搭配师 Id： " + collocationId);
	}
	collocation
		.setState(collocation.getState() == StylistConstants.State.NORMAL ? StylistConstants.State.DISABLE
			: StylistConstants.State.NORMAL);
	collocationService.updateByPrimaryKey(collocation);
	setInfoMsg("操作成功！", attr);
	String redirect = request.getParameter("redirect");
	if (StringUtils.isNotBlank(redirect)) {
	    return this.redirect(redirect);
	} else {
	    return this.redirect("/admin/collocation/bank");
	}
    }

    /**
     * 查找搭配师申请列表
     * 
     * @param pager
     * @param model
     * @param attr
     * @return
     */
    @RequestMapping("/list")
    public String list(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	if (null == pager) {
	    pager = new Pager();
	}
	Example example = new Example(Collocation.class);
	Criteria criteria = example.createCriteria();
	pager.setExample(example);
	collocationService.pager(pager);
	// PagerHelper.pager(pager,
	// collocationService.getCollocationExtMapper());
	model.put("pager", pager);
	return doPackageURI("collocation/list");
    }

    /**
     * 搭配师审核(此处信息需要与页面同步) 获取拒绝信息
     * 
     * @param type
     * @return
     */
    private String getRemark(String type) {
	String remark = "信息不符";
	if (type.equals("1")) {
	    remark = "身份证号错误";
	} else if (type.equals("2")) {
	    remark = "个人资历不符";
	} else if (type.equals("3")) {
	    remark = "身份证照片不符";
	}
	return remark;
    }
}
